#include "AppStateMenu.h"
#include "AppStateManager.h"
#include "Random.h"
#include <SDL.h>
void CAppStateMenu::OnKeyIpWrite(SDLKey sym)
{
		switch(sym)
		{
			//Erase letters one by one from the IP String
			case SDLK_BACKSPACE: 
			{
				if(!IP.empty()) 
					IP.erase(IP.length() - 1);
				break;
			} 
			//Go back to main menu
			case SDLK_ESCAPE:
			{
				mainChoice = MAINMENU_CHOICE;
				break;
			}
			//Go to Port write section if IP is valid
			case SDLK_RETURN:
			{      
				//Check to see if somewhat the IP adress is valid
				if(IP.empty() || (IP.length() < 16 && IP.length() > 5 && IP != "Not valid IP")) 
				{ 
					//If adress is empty go machine local
					if(IP.empty())
					{
						IP = "127.0.0.1";
					}
					//gamestate to lobby
					mainChoice = PORT_CHOICE;
				}
				else 
				{
					IP = "Not valid IP";
				}
				break;
			}
			default:
			{
				//if none of the above matches, write the inputed number to IP string
				if(IP.length() < MAX_IP_SIZE) 
				{
					if(sym == SDLK_PERIOD ||     
					   sym >= SDLK_0 && sym <= SDLK_9)   
					   IP += sym;        
				}
			}
		}
}


void CAppStateMenu::OnKeyPortWrite(SDLKey sym)
{
	switch(sym)
	{
		//Erase letters one by one from the Port String
		case SDLK_BACKSPACE: 
		{
			if(!port.empty()) 
				port.erase(port.length() - 1);
			break;
		} 
		//Go back to IPWrite
		case SDLK_ESCAPE:
		{
			mainChoice = JOIN_CHOICE;
			break;
		}
		case SDLK_RETURN:
		{      
			//Check to see if somewhat the IP adress is valid
			if(port.empty() || (port.length() < 5 && port.length() > 3)) 
			{ 
				//If Port number is empty write standard
				if(port.empty())
				{
					port = "7777";
				}
					
				//change gamestate to lobby
				CSoundBank::SoundControl.Stop();
				CAppStateManager::SetActiveAppState(APPSTATE_LOBBY);
					
			}
			else 
			{
				
			}
			break;
		}
		default:
		{
			//if none of the above matches, write the inputed number to Port string
			if(port.length() < 4) 
			{
				if(sym >= SDLK_0 && sym <= SDLK_9)   
					port += sym;        
			}
		}
	}
}

void CAppStateMenu::OnRenderJoin(SDL_Surface* Surf_Display)
{
	SDL_Rect NameRecti;
	NameRecti.x = 150;
	NameRecti.y = 150;
	NameRecti.w = 200;
	NameRecti.h = 30;

	SDL_Surface* Text_Solid;
	Text_Solid = NULL;
	SDL_Rect TextPos;
	TextPos.x = 204;
	TextPos.y = 258;

	SDL_Colour black = {0,0,0};
	if(mainChoice == JOIN_CHOICE)
	{
		Text_Solid = TTF_RenderText_Blended(IPFont, IP.c_str(), black);
	}
	else if(mainChoice == PORT_CHOICE)
	{
		Text_Solid = TTF_RenderText_Blended(IPFont, port.c_str(), black);
	}

	SDL_BlitSurface(Text_Solid, NULL, Surf_Display, &TextPos);

	//
	if(writeIP)
	{
		SDL_Rect IPBoxRect;
		IPBoxRect.x = 150;
		IPBoxRect.y = 150;
		IPBoxRect.w = 200;
		IPBoxRect.h = 30;
		
		SDL_Surface* Text_Solid;
		Text_Solid = NULL;
		SDL_Rect TextPos;
		SDL_Colour red = {0,0,0};
		SDL_BlitSurface(IPBox, NULL, Surf_Display, &IPBoxRect);
		if(mainChoice == JOIN_CHOICE)
		{
			TextPos.x = 204;
			TextPos.y = 210;
			Text_Solid = TTF_RenderText_Blended(menuChoiceFont, "Server adress", black);
			SDL_BlitSurface(Text_Solid, NULL, Surf_Display, &TextPos);
			SDL_FreeSurface(Text_Solid);
			TextPos.x = 204;
			TextPos.y = 258;
			Text_Solid = TTF_RenderText_Blended(IPFont, IP.c_str(), black);
			SDL_BlitSurface(Text_Solid, NULL, Surf_Display, &TextPos);
			SDL_FreeSurface(Text_Solid);
		}
		else if(mainChoice == PORT_CHOICE)
		{
			TextPos.x = 204;
			TextPos.y = 210;
			Text_Solid = TTF_RenderText_Blended(menuChoiceFont, "port adress", black);
			SDL_BlitSurface(Text_Solid, NULL, Surf_Display, &TextPos);
			SDL_FreeSurface(Text_Solid);
			TextPos.x = 204;
			TextPos.y = 258;
			Text_Solid = TTF_RenderText_Blended(IPFont, port.c_str(), black);
			SDL_BlitSurface(Text_Solid, NULL, Surf_Display, &TextPos);
			SDL_FreeSurface(Text_Solid);
		}
		else if(mainChoice == NAME_CHOICE)
		{
		 OnRenderNameChoice(Surf_Display);
		}
		
		
	}
}

